package com.example.mqspring.rabiitmqbasicleverone.rabbitmqtransaction;

import com.example.mqspring.utils.RabbitMqUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;

public class Producer {

    static private String message = "i want some money";

    public void configChannel(Channel channel, String queue) throws Exception {
        channel.queueDeclare(queue, true, false, false, null);
//        开启事物
        channel.txSelect();
    }

    public static void main(String[] args) {
        Producer producer = new Producer();
        Connection connection = RabbitMqUtil.createConnection();
        Channel channel = null;
        try {
            channel = connection.createChannel();
            producer.configChannel(channel, RabbitMqUtil.queue);
            //发送消息
            channel.basicPublish("",RabbitMqUtil.queue, MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes());
//           提交事物
            channel.txCommit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
